package com.tuanbiao.presenter.setting;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.base.CapWorkMode;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.constants.Constants;
import com.tuanbiao.constants.SettingType;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.events.LoginValidEvent;
import com.tuanbiao.utils.SPHelper;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/15 15:25
 */
public class SettingPresenter extends BasePresenter<SettingController.Model, SettingController.View> implements SettingController.Presenter {
    private CapWorkMode mCapWorkMode;

    public SettingPresenter(SettingController.Model model, SettingController.View rootView) {
        super(model, rootView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginStateChange(LoginValidEvent event) {
        mRootView.showLoginTime(event.isLoginValid() ? "" : "登录已失效");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void init() {
        mRootView.showLoginTime(SPHelper.isLoginValid() ? "" : "登录已失效");
        mCapWorkMode = mModel.getSettingParams().getCapWorkMode();
        mRootView.showWorkModel(getWorkModeName(mCapWorkMode));
        mModel.getLoginSurplusTime(time -> {
            Timber.i(TAG + "#time:" + time);
            mRootView.showLoginTime(time);
        });
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private void updateVisibility() {
        mRootView.showWorkModelChange(SPHelper.showSettingWorkModel());
        mRootView.showControlPanelChange(SPHelper.showSettingControlPanel());
        mRootView.showServerChange(SPHelper.showSettingServerChange());
        mRootView.showLocalChange(SPHelper.showSettingLocalChange());
        mRootView.showDeviceNumChange(SPHelper.showDeviceNumChange());
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public void onDestroy() {
        mModel.removeLoginSurplusTime();
        super.onDestroy();
    }

    @Override
    public void toggleModel() {
        if (SPHelper.showSettingWorkModel()) {
            if (mCapWorkMode == CapWorkMode.HIGHTEMP) {
                mCapWorkMode = CapWorkMode.NORMAL;
            } else {
                mCapWorkMode = CapWorkMode.HIGHTEMP;
            }
            mModel.setHighModel(mCapWorkMode, aBoolean -> mRootView.showWorkModel(getWorkModeName(mCapWorkMode)));
        }
    }

    @Override
    public void toggleLoginTime() {

    }

    @Override
    public void logout() {
        mModel.logout();
        mRootView.startLogin();
    }

    @Override
    public void checkPassword(String content, String password, SettingType type) {
        if (TextUtils.equals(content, password)) {
            switch (type) {
                case CHANGE_DEVICE_NUM:
                    mRootView.startChangeDeviceNum();
                    break;
                case CHANGE_SERVICE:
                    mRootView.startChangeService();
                    break;
                default:
                    break;
            }

        } else {
            mRootView.showMessage("密码错误");
        }
    }

    @Override
    public String getPassword() {
//        return SettingUtils.createPassword();
        return "123456";
    }

    private String getWorkModeName(CapWorkMode capWorkMode) {
        if (capWorkMode == CapWorkMode.HIGHTEMP) {
            return Constants.WorkMode.HIGH_TEMP;
        } else {
            return Constants.WorkMode.NORMAL;
        }
    }
}
